
var Utils = {}

Utils.log = function(msg){
	Application.console.log("flashcard extension: " + msg);
}

Utils.compose_urlparamstr = function(dict){
	var urlparams = [];
	for (var p in dict) {
		urlparams.push(p + '=' + encodeURIComponent(dict[p]));
	}
	return urlparams.join('&');
}
	
Utils.decompose_urlparamstr = function(str) {
	var params = {};
	var urlparams = str.split('&');
	for (var i in urlparams) {
		var x = urlparams[i].split('=');
		params[x[0]] = x[1];
	}
	return params;
}
	
Utils.getSelection = function(popupnode) {
	Utils.log("Start to select word.")
	var nodeLocalName = popupnode.localName.toLowerCase();
  	var selection = '';

	if ((nodeLocalName == "textarea") || (nodeLocalName == "input" && popupnode.type == "text")) {

	    selection = popupnode.value.substring(popupnode.selectionStart, popupnode.selectionEnd);

	} else if (nodeLocalName == "img") {
	    if (popupnode.title) {
	        selection = popupnode.title;
	    } else if (popupnode.alt) {
	        selection = popupnode.alt;
	    }

	} else if (nodeLocalName == "a" && popupnode.hasAttribute("href") && (popupnode.textContent != "" || popupnode.hasAttribute("title"))) {

	    if (popupnode.textContent != "") {
	        selection = popupnode.textContent;

	    } else if (popupnode.hasAttribute("title")) {
	        selection = popupnode.getAttribute("title");
	    }

	} else {
	    selection = document.commandDispatcher.focusedWindow.getSelection().toString();
	}

	return selection;
}